<?php
abstract class prices
{	var $tables;	function getTables($firms_name)
	{
		$forbidden=array('`', '~', '&', '^', '*', '{}');
		$countTabs=count($_POST['countTabs']);
		foreach($_POST as $key=>$vol)
		{			if($key=='countTabs' || $key=='imgToDelete' || $key=='cityId')continue;
			$_POST[$key]=str_replace($forbidden, '', $vol);
		}
		$_POST['tab_name']=mb_substr($_POST['tab_name'], 0, 15, 'utf-8');
		$tab=array();
		for($m=1; $m<=$countTabs; $m++)
		{
			$table=$m;
			$rowNum=$_POST['rowNum'.$table];
			$colNum=$_POST['colNum'.$table];
			$header=array();
			$tr=array();
			for($j=11; $j<=($rowNum+10); $j++)
			{
				$trId=$table.$j;
				$td=array();
				for($i=1; $i<=$colNum; $i++)
				{
					$_POST[$table.$j.$i]=substr($_POST[$table.$j.$i], 0, 400);
					$td[]=$_POST['colType'.$table.$i].'^'.$table.$j.$i.'^'.$_POST[$table.$j.$i];
					$header[$table.$i]=$_POST['header'.$table.$i];
					if($_POST['colType'.$table.$i]=='img')
					{						if(!$_POST['cityId'])
						{
							if(file_exists($_POST[$table.$j.$i].'.jpg'))
							{
								if(!file_exists('pictures/pricepics/'.$_GET['city'].'/'.$_GET['id'].'/'))
								{
									mkdir('pictures/pricepics/'.$_GET['city'].'/'.$_GET['id'].'/', 0777, true);
								}
								rename($_POST[$table.$j.$i].'.jpg',
								'pictures/pricepics/'.$_GET['city'].'/'.$_GET['id'].'/'.$table.$j.$i.'.jpg');
								$products[$table.$j]['photo']=$table.$j.$i;
							}
							if(!$_POST[$table.$j.$i] && file_exists('pictures/pricepics/'.$_GET['city'].'/'.$_GET['id'].'/'.$table.$j.$i.'.jpg'))
								unlink('pictures/pricepics/'.$_GET['city'].'/'.$_GET['id'].'/'.$table.$j.$i.'.jpg');
						}
						else
						{							if(file_exists($_POST[$table.$j.$i].'.jpg'))
							{
								if(!file_exists('pictures/netspricepics/'.$_GET['id'].'/'))
								{
									mkdir('pictures/netspricepics/'.$_GET['id'].'/', 0777, true);
								}
								rename($_POST[$table.$j.$i].'.jpg',
								'pictures/netspricepics/'.$_GET['id'].'/'.$table.$j.$i.'.jpg');
								$products[$table.$j]['photo']=$table.$j.$i;
								$photoInfo[]=$table.$j.$i;
							}
							if(!$_POST[$table.$j.$i] && file_exists('pictures/netspricepics/'.$_GET['id'].'/'.$table.$j.$i.'.jpg'))
								unlink('pictures/netspricepics/'.$_GET['id'].'/'.$table.$j.$i.'.jpg');	  					}
					}
					if($_POST['colType'.$table.$i]=='name' || $_POST['colType'.$table.$i]=='brend' ||
					$_POST['colType'.$table.$i]=='model' || $_POST['colType'.$table.$i]=='price')
						$products[$table.$j][$_POST['colType'.$table.$i]]=$_POST[$table.$j.$i];
				}
				$tr[]=$trId.'&'.implode('|', $td);
			}
			$tab[]=$_POST['tblName'.$table].'~'.$_POST['border'.$table].'~'.$table.'~'.implode('*', $tr).'~'.implode('{}', $header);
		}
		$tables=implode('`', $tab);
		if($products)
		{			foreach($products as $k=>$product)
			{
				$data=array();
				foreach($product as $field=>$value)
				{
					if($value)$data[]=$field."='".$value."'";
				}
				if($data && !$_POST['cityId'])$aProduct[]=implode(', ', $data).", firms_id='".$_GET['id']."', firms_name='".$firms_name."'";
				else if($_POST['cityId'] && $data)
					$aProduct[]=implode(', ', $data).", firms_name='".$firms_name."'";

			}
		}
		if($aProduct && !$_POST['cityId'])
		{
			$aConnect=new connect('pr');
			$query="DELETE FROM ".$_GET['city']." WHERE firms_id='".$_GET['id']."'";
			mysql_query($query) or die(mysql_error());
			if($aProduct)
			{
				foreach($aProduct as $key=>$dataToInsert)
					$aConnect->insert_data($_GET['city'], $dataToInsert);
			}
		}
		$this->tables=$tables;
		if($aProduct && $_POST['cityId'])
		{
			$aConnect=new connect('pr');
			for($j=0; $j<count($_POST['cityId']); $j++)
			{
				$cityIdEls=explode('^', $_POST['cityId'][$j]);
				$city=$cityIdEls[0];
				$id=$cityIdEls[1];
				$query="DELETE FROM ".$city." WHERE firms_id='".$id."'";
				mysql_query($query) or die(mysql_error());
				if($aProduct)
				{
					foreach($aProduct as $key=>$dataToInsert)
					{						$dataToInsert=$dataToInsert.", firms_id='".$id."'";
						$aConnect->insert_data($city, $dataToInsert);
					}
				}
			}
		}
		if($photoInfo && $_POST['cityId'])
		{			for($j=0; $j<count($_POST['cityId']); $j++)
			{				$cityIdEls=explode('^', $_POST['cityId'][$j]);
				$city=$cityIdEls[0];
				$id=$cityIdEls[1];
				//удалить все прайсфотки в этом городе
				if(file_exists('pictures/pricepics/'.$city.'/'.$id.'/'))
				{					$dir=opendir('pictures/pricepics/'.$city.'/'.$id.'/');
					while($file=readdir($dir))
					{						if($file=='.' || $file=='..')continue;						if(file_exists('pictures/pricepics/'.$city.'/'.$id.'/'.$file))
							unlink('pictures/pricepics/'.$city.'/'.$id.'/'.$file);					}				}
				for($i=0; $i<count($photoInfo); $i++)
				{					$photo=$photoInfo[$i];					if(file_exists('pictures/netspricepics/'.$_GET['id'].'/'.$photo.'.jpg'))
					{						if(!file_exists('pictures/pricepics/'.$city.'/'.$id.'/'))
							mkdir('pictures/pricepics/'.$city.'/'.$id.'/', 0777, true);
						//echo 'pictures/pricepics/'.$city.'/'.$id.'/'.$photo.'.php';						/*$file='pictures/pricepics/'.$city.'/'.$id.'/'.$photo.'.php';
						$photoSrcFile=fopen($file, "w");
						fwrite($photoSrcFile, "<?php echo 'pictures/netspricepics/".$_GET['id']."/".$photo.".jpg'; ?>");
						fclose ($photoSrcFile); */
						copy('pictures/netspricepics/'.$_GET['id'].'/'.$photo.'.jpg', 'pictures/pricepics/'.$city.'/'.$id.'/'.$photo.'.jpg');
					}				}
			}		}
		return $this->tables;
	}
	function showTables($tables, $edit=false, $temp='', $limit=array(), $nets_id='')
	{
		$tab=explode('`', $tables);
		if(isset($limit))
		{			$limitStart=$limit[1];			$limit=$limit[0];
		}
		for($m=1; $m<=count($tab); $m++)
		{
			if($limit || $limit===0)
			{
				if($m<$limitStart || $m>($limit+$limitStart-1))continue;
			}
			$table=$m;
			$colType=array();
			$header=array();
			if($isHeaderEmpty)$header=false;
			$tabEls=explode('~', $tab[$m-1]);
			$borderChecked=false;
			if($tabEls[1]=="ON")
			{				$border=1;
				$borderChecked='checked';
			}
			else $border=0;
			$tr=explode('*', $tabEls[3]);
			$header=explode('{}', $tabEls[4]);
			echo ('
			<div class="aTable">
			<center>
			');
			$this->showTabName($tabEls[0], $table);
			echo ('
			<input name="countTabs[]" class="countTabs" type="hidden" value="'.$tabEls[2].'">
			');
			if($edit)
			{				echo ('
				Показывать границы таблицы<input name="border'.$tabEls[2].'" type="checkbox" value="ON" '.$borderChecked.'>
				');
			}
			echo ('
			<table id="table'.$tabEls[2].'" class="priceTable" cellspacing="0" border="'.$border.'">
			');
			for($j=11; $j<=(count($tr)+10); $j++)
			{
				$trId=$table.$j;
				$trEls=explode('&', $tr[$j-11]);
				$td=explode('|', $trEls[1]);
				for($i=1; $i<=count($td); $i++)
				{
					$tdEls=explode('^', $td[$i-1]);
					$colType[$table.$i]=$tdEls[0];
				}
			}
			$this->showColType($table, $colType);
			$this->showHeader($table, $header);
			for($j=11; $j<=(count($tr)+10); $j++)
			{
				$trId=$table.$j;
				$trEls=explode('&', $tr[$j-11]);
				$td=explode('|', $trEls[1]);
				echo ('
				<tr id="'.$trEls[0].'">
				');
				for($i=1; $i<=count($td); $i++)
				{
					$tdEls=explode('^', $td[$i-1]);
					echo ('
					<td class="'.$tdEls[0].'" id="'.$tdEls[1].'">
					');
					$this->showTd($tdEls[0], $tdEls[1], $tdEls[2], $temp);
					echo ('
					</td>
					');
				}
				if($edit)
				{					if($i<=5)
					{						$id=$i;						if($j==11)
						{							echo ('
							<td id="'.$table.'11'.$id.'" class="addColButton">
								Добавить столбец
							</td>
							');
						}
						else
						{
							echo ('
							<td id="'.$table.$j.$id.'" class="add">
							</td>
							');
						}					}
					if(count($tr)>1)
					echo('
					<td class="delete">
						Удалить
					</td>
					');				}
				echo ('
				</tr>
				');
			}
			if($edit)
			{				$trId=$table.($j);				echo ('
				<tr id="'.$trId.'">
				');
				for($i=1; $i<=(count($td)+1); $i++)
				{					if($i==1)
					{						echo ('
						<td id="'.$trId.'1" class="addRowButton">
							Добавить ряд
						</td>
						');
					}
					else
					{						echo ('
						<td id="'.$trId.$i.'" class="add">
						</td>
						');
					}
				}
					echo ('
				</tr>
				');			}
			echo ('
			</table>
   			<input name="colNum'.$table.'" id="colNum'.$table.'" type="hidden" value="'.count($td).'">
			<input name="rowNum'.$table.'" id="rowNum'.$table.'" type="hidden" value="'.count($tr).'">
			');
			if($edit)
			{				echo ('
				<div class="delTabButton" id="delete'.$table.'">Удалить таблицу</div>
				');			}
			if($edit && $m==count($tab))
			{
				echo ('
				<div class="addButton" id="add'.($table+1).'">Создать еще таблицу</div>
				');
			}
			echo ('
			</center>
			</div>
			');
		}
	}
	abstract function showTabName($tabName, $table);
	abstract function showColType($table, $colType);
	abstract function showHeader($table, $header);
	abstract function showTd($tdClass, $tdId, $tdVol, $temp='');}
?>